﻿using CommunityToolkit.Mvvm.Messaging;
using Company.Core.Config;
using Company.Core.Helper;
using Company.Sqlite.Interfaces;
using Company.Sqlite.Models;
using MahApps.Metro.Controls;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using 无人值守地磅称重系统.Interfaces;
using 无人值守地磅称重系统.Models.Login;
using 无人值守地磅称重系统.Models.Message;
using 无人值守地磅称重系统.ViewModels;

namespace 无人值守地磅称重系统.Views
{
    /// <summary>
    /// ShellView.xaml 的交互逻辑
    /// </summary>
    public partial class ShellView : MetroWindow
    {
        private ISession Session { get; }
        private IHardwareManager HardwareManager { get; }

        public ShellView(IUserRepository userRepository, 
            IHardwareManager hardwareManager,
            ISession session, 
            IConfigManager configManager)
        {
            InitializeComponent();
            Session = session;
            HardwareManager = hardwareManager;
            Session.Title = string.Empty;
            DataContext = App.Current.Services.GetService<ShellViewModel>();

            User user = userRepository.Select("admin");
            if(user== null)
            {
                user = new User();
                user.UserName = "admin";
                user.Password= "12345678";
                user.Role = 0; // 0代表超级管理员
                user.InsertDate = DateTime.Now;
                var count = userRepository.Insert(user);
                if(count>0)
                {
                    MessageBox.Show($"首次运行系统，注册管理员成功！\r\n用户名：admin\r\n密码：12345678");
                }
            }
            container.Content = App.Current.Services.GetService<LoginView>(); // 启动时默认显示登录页面

            // 登录成功后， 跳转到首页
            WeakReferenceMessenger.Default.Register<LoginSuccessMessage>(this, (sender, arg) =>
            {
                configManager.Write(ConfigKey.LoginUser, new LoginUser { UserName = arg.Value.UserName, LoginDate = DateTime.Now });
                Session.CurrentUser = arg.Value;
                Session.Title = "无人值守地磅称重系统 — 东友 潘斌";
                container.Content = App.Current.Services.GetService<MainView>();
                WindowState = WindowState.Normal;
                //Width = 1880;
                //Height = 1000;
                Width = SystemParameters.WorkArea.Width -100; // 自适应屏幕宽度
                Height = SystemParameters.WorkArea.Height -60; // 自适应屏幕高度
                setWindow();
            });

            // 切换用户
            WeakReferenceMessenger.Default.Register<LogoutMessage>(this, (sender, arg) =>
            {
                //Log.Info("切换用户，关闭硬件连接");
                //HardwareManager.Close();
                Session.Title = string.Empty;
                Session.CurrentUser = new User();
                container.Content = App.Current.Services.GetService<LoginView>();
                WindowState = WindowState.Normal;
                Width = 800;
                Height = 450;
                setWindow();
            });

            // 退出应用前关闭硬件连接
            WeakReferenceMessenger.Default.Register<ExitMessage>(this, (sender, arg) =>
            {
                Log.Info(arg.Value);
                HardwareManager.Close();
            });
        }

        private void setWindow()
        {
           Left = (SystemParameters.WorkArea.Width- Width) / 2;
           Top = (SystemParameters.WorkArea.Height- Height) / 2;
        }
    }
}
